<?php
/*dentro la var metto il contenuto della scheda*/
global $DS_URI;
global $HOST;

if (!is_array ($_POST) || $_POST['scheda'] == "") {
	echo DSError ("400", "Scheda vuota");
	die;
}

/*valido*/
$domDoc=new DOMDocument("1.0","UTF-8");

error_reporting(0);
if (!$domDoc->loadXML(urldecode(stripslashes($_POST['scheda'])))) {
	echo DSError("400","scheda non corretta");
	die;
}
$pathSchemaScheda="http://vitali.web.cs.unibo.it/twiki/pub/TechWeb08/PagSchemaDS/schedaSchema.xsd";
//$pathSchemaScheda="../../data/ltw0807/ds/Schemas/schedaSchema.xsd";

if(!$domDoc->schemaValidate($pathSchemaScheda)){
	/*$docRit=new DOMDocument("1.0","UTF-8");
	$risp=$docRit->createElement('risposta');
	$risp=$docRit->appendChild($risp);*/
	echo DSError("400","La scheda non ha passato la validazione.");
	/*$err = new DOMDocument("1.0","UTF-8");
	$err->loadXML($errs);
	$err=$err->getElementsByTagName('errore')->item(0);
	$err=$docRit->importNode($err,true);
	$risp->appendChild($err);
	print $docRit->saveXML();*/
	die;
}

/*variabili*/

$id=manageId();
$EXPRESSION_URI = "http://{$HOST}{$DS_URI}/expression/{$id}";
$EXPRESSION_PATH = "../../html{$DS_URI}/item/{$id}.xml";

$data=date("Y-m-d H:i:s");
$data=str_replace(" ","T",$data);
$publisher="http://{$HOST}{$DS_URI}/catalog.xml";

/*piglio il dom e cambio i 5 valori*/
$xPath=new DOMXPath($domDoc);
$query='//wdate';
$nodo=$xPath->query($query)->item(0);
$nodo->nodeValue=$data;

$query='//edate';
$nodo=$xPath->query($query)->item(0);
$nodo->nodeValue=$data;

$query='//epublisher';
$nodo=$xPath->query($query)->item(0);
$nodo->nodeValue=$publisher;

$query='//widentifier';
$nodo=$xPath->query($query)->item(0);
if($nodo->nodeValue=="0"){
	$nodo->nodeValue="{$EXPRESSION_URI}";
	$query="//esource";
	$nodo=$xPath->query($query)->item(0);
	$nodo->nodeValue="{$EXPRESSION_URI}";
}

$query='//eidentifier';
$nodo=$xPath->query($query)->item(0);
$nodo->nodeValue="{$EXPRESSION_URI}";

write_xml_file ($domDoc, $EXPRESSION_PATH);
$indice = new DOMDocument("1.0","UTF-8");
$indice->load('../../data/ltw0807/ds/index.xml');
$toAdd=$domDoc->getElementsByTagName('metadati')->item(0);
$toAdd=$indice->importNode($toAdd,true);
$indice->getElementsByTagName('index')->item(0)->appendChild($toAdd);
write_xml_file ($indice, '../../data/ltw0807/ds/index.xml');

if($indice==false){
	echo DSError("500","errore nel salvataggio dell'indice"); 
	die;
}

/*ritorno un documento di tipo risposta*/
$docRit=new DOMDocument("1.0","UTF-8");
$risp=$docRit->createElement('risposta');
$risp->nodeValue=$EXPRESSION_URI;
$risp=$docRit->appendChild($risp);
output_typed_string ($docRit->saveXML(), "application/xml");

/*
 * Legge e aggiorna l'identificativo della scheda
 */

function manageId(){
	$domAppo=new DOMDocument("1.0","UTF-8");
	$domAppo->load('../../data/ltw0807/ds/lastPostedId.xml');
	$xPathApp=new DOMXPath($domAppo);
	$query='//valore';
	$nodoAppo=$xPathApp->query($query);
	$id=$nodoAppo->item(0)->nodeValue;
	$id++;
	$domAppo->replaceChild(($domAppo->createElement('valore',$id)),($domAppo->getElementsByTagName('valore')->item(0)));
	$domAppo->save('../../data/ltw0807/ds/lastPostedId.xml');

	return $id;
}



?>
